﻿#region 信息

// Copyright©2014 Rex Rao all rights reserved.
// File name : SoMcCoffee\Sohobloo.SoCommon\DateTimeExtension.cs
// Author : Sohobloo
// Create time : 2014/01/31 23:40
// Update time : 2014/02/05 8:54

#endregion

#region 引用

using System;

#endregion

namespace Sohobloo.SoCommon.CommonExtension
{
    public static class DateTimeExtension
    {
        public const DayOfWeek TheFirstDayOfWeek = DayOfWeek.Monday;

        public static bool IsYesterday(this DateTime date)
        {
            return date.Date.AddDays(1) == DateTime.Today;
        }

        public static bool IsToday(this DateTime date)
        {
            return date.Date == DateTime.Today;
        }

        public static bool IsTomorrow(this DateTime date)
        {
            return date.Date.AddDays(-1) == DateTime.Today;
        }

        private static int GetDayOfWeek(this DateTime date, DayOfWeek theFirstDayOfWeek = TheFirstDayOfWeek)
        {
            return ((int)date.DayOfWeek - (int)theFirstDayOfWeek + 7) % 7;
        }

        public static bool IsInSameWeek(this DateTime firstDate, DateTime secondDate,
            DayOfWeek theFirstDayOfWeek = TheFirstDayOfWeek)
        {
            var daySpan = (secondDate.Date - firstDate.Date).TotalDays;
            if(daySpan >= 7d || daySpan <= -7d)
            {
                return false;
            }

            var fisrtDayOfWeek = firstDate.GetDayOfWeek(theFirstDayOfWeek);
            var secondDayOfWeek = secondDate.GetDayOfWeek(theFirstDayOfWeek);

            return (daySpan >= 0) ^ (fisrtDayOfWeek >= secondDayOfWeek);
        }

        public static bool IsLastWeek(this DateTime date, DayOfWeek theFirstDayOfWeek = TheFirstDayOfWeek)
        {
            return date.AddDays(7).IsInSameWeek(DateTime.Today, theFirstDayOfWeek);
        }

        public static bool IsThisWeek(this DateTime date, DayOfWeek theFirstDayOfWeek = TheFirstDayOfWeek)
        {
            return date.IsInSameWeek(DateTime.Today, theFirstDayOfWeek);
        }
    }
}